package com.ruoyi.mall.app.service.impl;

import com.ruoyi.mall.app.service.DtsGoodsSpecificationService;
import com.ruoyi.mall.common.domain.DtsGoodsSpecification;
import com.ruoyi.mall.common.mapper.DtsGoodsSpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DtsGoodsSpecificationServiceImpl implements DtsGoodsSpecificationService {

    @Autowired
    DtsGoodsSpecificationMapper dtsGoodsSpecificationMapper;

    @Override
    public List<DtsGoodsSpecification> selectGoodsSpecificationCountByGoodsId(int goodsId) {
        return dtsGoodsSpecificationMapper.selectGoodsSpecificationCountByGoodsId(goodsId);
    }

    @Override
    public List<DtsGoodsSpecification> selectGoodsSpecificationByGoodsId(int goodsId, List<DtsGoodsSpecification> dtsGoodsSpecification) {
        // 获取规格信息
        List<DtsGoodsSpecification> goodsSpecificationList = dtsGoodsSpecificationMapper.selectGoodsSpecificationByGoodsId(goodsId);
        List<DtsGoodsSpecification> dtsGoodsSpecificationList = new ArrayList<>();  // 返回值
        long count = 0;
        long a = 0;
        for (DtsGoodsSpecification goodsSpecification : dtsGoodsSpecification) {
            DtsGoodsSpecification dtsGoodsSpecification1 = new DtsGoodsSpecification();
            dtsGoodsSpecification1.setSpecification(goodsSpecification.getSpecification());
            count += goodsSpecification.getCount();
            for (long i = a; i < count; i++) {
                dtsGoodsSpecification1.getGoodsSpecificationList().add(goodsSpecificationList.get((int) i));
            }
            a += count;
            dtsGoodsSpecificationList.add(dtsGoodsSpecification1);
        }
        return dtsGoodsSpecificationList;
    }
}
